package me.yobol.datastructure.basic.list.linked.sll;

import com.sun.org.apache.bcel.internal.generic.DDIV;
import com.sun.org.apache.bcel.internal.generic.GOTO;
import me.yobol.datastructure.basic.list.List;
import me.yobol.datastructure.basic.list.Node;

/**
 *
 * @author Yobol
 */
public class SingleLinkedList implements List{

    private SingleLinkedNode root;
    private int size;
    
    public SingleLinkedList(){
    }
    
    @Override
    public void init() {
        root = new SingleLinkedNode();
        this.size = 0;
    }

    /**
     * 
     * @return
     * @throws Exception 
     */
    @Override
    public boolean isEmpty() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        return (root.getNext() == null);
    }

    @Override
    public boolean isFull() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        return false;
    }

    /**
     * 
     * @return
     * @throws Exception 
     */
    @Override
    public int size() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        return this.size;
    }

    @Override
    public void insert(int index, Node node) throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        if (index < 1 || index > size + 1) {
            throw new Exception("下标越界");
        }
        int counter = 0;
        SingleLinkedNode current = this.root;
        SingleLinkedNode prior = this.root;
        while(current != null){
            prior = current;
            if (++counter == index) {
                ((SingleLinkedNode)node).setNext(prior.getNext());
                prior.setNext((SingleLinkedNode)node);
                size++;
            }
            current = current.getNext();
        }
    }

    /**
     * 删除指定位置上的结点并返回
     * 
     * @param node
     * @throws Exception 
     */
    @Override
    public void insert(Node node) throws Exception {
        insert(size + 1, node);
    }

    @Override
    public Node delete(int index) throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        if (index < 1 || index > size) {
            throw new Exception("下标越界");
        }
        SingleLinkedNode target = null;
        SingleLinkedNode current = this.root;
        SingleLinkedNode prior = this.root;
        int counter = 0;
        
        while (current != null) { 
            if(counter++ == index){
                target = current;
                prior.setNext(current.getNext());
                size--;
            }
            prior = current;
            current  = current.getNext();
        }
        return target;
    }

    @Override
    public Node getByLocale(int index) throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        if (index < 1 || index > size) {
            throw new Exception("下标越界");
        }
        SingleLinkedNode target = null;
        SingleLinkedNode current = this.root;
        int counter = 0;
        while ((current = current.getNext()) != null) {            
            if (++counter == index) {
                target = current;
            }
        }
        return target;
    }

    @Override
    public Node getByKey(int key) throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        SingleLinkedNode target = null;
        SingleLinkedNode current = this.root;
        while ((current = current.getNext()) != null) {            
            if (current.getKey() == key) {
                target = current;
            }
        }
        return target;
    }

    
    @Override
    public void print() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        System.out.println("--------------------------------");
        System.out.println("size : " + size);
        System.out.println("--------------------------------");
        SingleLinkedNode temp = this.root;
        int counter = 0;
        while ((temp = temp.getNext()) != null) {
            System.out.println(++counter + " : " + temp.getKey());
        }
    }

    /**
     * 
     * @throws Exception 
     */
    @Override
    public void clear() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        this.root.setNext(null);
    }

    /**
     * 
     * @throws Exception 
     */
    @Override
    public void destroy() throws Exception {
        if (root == null) {
            throw new Exception("根结点未初始化");
        }
        this.root = null;
    }
    
}
